{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用time库来替代某个程序 的进行过程，做实例，\n",
    "\n",
    "思路是，简单打印出来程序进度\n",
    "\n",
    "单行刷新关键是\\r，\n",
    "\n",
    "python默认是print后换行，所以加一个\\r是光标回退到之前位置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 方式一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "********开始********\n",
      "100%[##################################################->]\n",
      "********结束********\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "import sys\n",
    "\n",
    "total = 50\n",
    "print('{:*^18}'.format('开始'))\n",
    "for i in range(total + 1):\n",
    "    a = '#' * i\n",
    "    b = '.' * (total - i)\n",
    "    c = (i / total) * 100\n",
    "    print('\\r{:^3.0f}%[{}->{}]'.format(c, a, b), end=\"\")\n",
    "    # sys.stdout.write('\\r{:^3.0f}%[{}->{}]'.format(c, a, b))\n",
    "    time.sleep(0.1)  # 这是模拟程序进程，可将time.sleep替换成程序进程\n",
    "print('\\n{:*^18}'.format('结束'))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# 方式二\n",
    "\n",
    "显示执行时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------执行开始----------\n",
      "100%[##################################################->]5.06s\n",
      "-----------执行结束----------\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "total = 50\n",
    "print(\"执行开始\".center(scale // 2, \"-\"))\n",
    "start = time.perf_counter()\n",
    "for i in range(total + 1):\n",
    "    a = \"#\" * i\n",
    "    b = \" \" * (total - i)\n",
    "    c = (i / total) * 100\n",
    "    dur = time.perf_counter() - start\n",
    "    print(\"\\r{:^3.0f}%[{}->{}]{:.2f}s\".format(c, a, b, dur), end=\"\")\n",
    "    time.sleep(0.1)\n",
    "print(\"\\n\" + \"执行结束\".center(scale // 2, \"-\"))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 方式三"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "120%[############################################################->]"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import time\n",
    "\n",
    "\n",
    "def view_bar(num, total, length=50):\n",
    "    \"\"\"\n",
    "    :param length :进度条长度\n",
    "    \"\"\"\n",
    "    rate = num / total\n",
    "    a = \"#\" * int(rate * length)\n",
    "    b = \" \" * int(length - rate * length)\n",
    "    c = rate * 100\n",
    "    sys.stdout.write('\\r{:^3.0f}%[{}->{}]'.format(c, a, b))\n",
    "    sys.stdout.flush()\n",
    "\n",
    "# 任务数\n",
    "total = 5\n",
    "for i in range(1, total + 2):\n",
    "    time.sleep(1)\n",
    "    view_bar(i, total)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
